package com.fish.middleware.minio.service;

import com.fish.common.config.FishConfig;
import com.fish.common.core.domain.entity.FileEntity;
import com.fish.common.service.file.FileService;
import com.fish.common.utils.file.FileOperateUtils;
import com.fish.common.utils.file.FileUtils;
import com.fish.middleware.minio.config.MinioClientProperties;
import com.fish.middleware.minio.config.MinioConfig;
import com.fish.middleware.minio.domain.MinioFileVO;
import com.fish.middleware.minio.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;

/**
 * Minio文件操作实现类
 */
@Component("file:strategy:minio")
@ConditionalOnProperty(prefix = "minio", name = {"enable"}, havingValue = "true", matchIfMissing = false)
public class MinioFileService implements FileService {
    @Autowired
    private MinioConfig minioConfig;

    @Override
    public String upload(String filePath, MultipartFile file) throws Exception {
        String relativePath = null;
        if (FileUtils.isAbsolutePath(filePath)) {
            relativePath = FileUtils.getRelativePath(filePath);
        } else {
            relativePath = filePath;
        }
        // 判断系统类型转换文件上传格式
        MinioClientProperties properties = minioConfig.getClient().get(minioConfig.getPrimary());
        if ("linux".equals(properties.getSystemType())) {
            relativePath = relativePath.replace("\\", "/");
        } else {
            relativePath = relativePath.replace("/", "\\");
        }
        return MinioUtil.uploadFile(minioConfig.getPrimary(), relativePath, file);
    }

    @Override
    public String upload(MultipartFile file, String name) throws Exception {
        return MinioUtil.uploadFile(minioConfig.getPrimary(), name, file);
    }

    @Override
    public String upload(MultipartFile file) throws Exception {
        String filePath = FishConfig.getProfile() + File.separator + FileUtils.fastFilePath(file);
        return upload(filePath, file);
    }

    @Override
    public String upload(String baseDir, String fileName, MultipartFile file) throws Exception {
        return upload(baseDir + File.pathSeparator + fileName, file);
    }

    @Override
    public InputStream downLoad(String filePath) throws Exception {
        MinioFileVO file = MinioUtil.getFile(minioConfig.getPrimary(), filePath);
        return file.getFileInputSteam();
    }

    @Override
    public boolean deleteFile(String filePath) throws Exception {
        MinioUtil.removeFile(minioConfig.getPrimary(), filePath);
        FileOperateUtils.deleteFileAndMd5ByFilePath(filePath);
        return true;
    }

    /**
     * 获取文件
     *
     * @param filePath
     * @return
     * @throws Exception
     */
    @Override
    public FileEntity getFile(String filePath) throws Exception {
        return MinioUtil.getFile(minioConfig.getPrimary(), filePath);
    }

    ;
}
